package Common;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Utils {

	private WebDriver driver;
	
	public Utils(WebDriver dd) {
		driver=dd;
	}
	
	public void waitForElementPresent(final By by) throws Exception {
		try {
			new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
	            public Boolean apply(WebDriver webDriver) {
	                return isElementPresent(by);
	            }
	        });
		} catch (NoSuchElementException e) {
			throw new Exception("Nie znaleziono elementu: " + by.toString());
		}
	}
	
	public void waitForElementNotPresent(final By by) throws Exception {
		try {
			
			new WebDriverWait(driver, 10, 1)
			.until(new ExpectedCondition<Boolean>() {
				int licznik=0;
				public Boolean apply(WebDriver d) {
					Boolean czy_obecny = isElementPresent(by);
					System.out.println("Licznik: "+licznik);
					licznik++;
					if(!czy_obecny) return true;
					else return false;
				}
			});
			
			
		} catch (NoSuchElementException e) {
			throw new Exception("Nie znaleziono elementu: " + by.toString());
		}
	}
	
	public boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
	
}
